package net.superdvach.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class HomeHandler implements HttpHandler {

	public HomeHandler(WebClient parent) {
		m_parent = parent;
	}

	@Override
	public void handle(HttpExchange exchange) throws IOException {
		PrintWriter out = new PrintWriter(exchange.getResponseBody());
		exchange.sendResponseHeaders(200, 0);
		Template tpl = m_parent.templateEngine().getTemplate("home.htpl");
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("s2ch", m_parent.dataRoot());
			tpl.process(map, out);
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.close();
	}

	private WebClient m_parent;

}
